package com.kaifamiao.Practice;

public class LeonardodaFibonacci {

    public static void main(String[] args) {


        LeonardodaFibonacci fb = new LeonardodaFibonacci();
        System.out.println(fb.fb(10));
        System.out.println(fb.Fibonacci(10));
        System.out.println(fb.Fibonacci(10));
        System.out.println(fb.Fibonacci2(10));
        //1.定义三个变量,用来存放第一个第二第三个斐波那契数
        int a1 = 1;
        int a2 = 1;
        int a3;
        System.out.print(a1 + "," + a2);
        for (int i = 3; i <= 10; i++) {
            //2.根据前两个数算出第三个数
            a3 = a2 + a1;
            //3.更新第一第二个数
            a1 = a2;
            a2 = a3;
            System.out.print("," + a3);
        }
    }

    public long fb(long n) {


        long prePre = 1;
        long pre = 1;
        long feibo = 0;
        if (n == 1 || n == 2) {
            return 1;
        }

        return feibo = fb(n - 1) + fb(n - 2);

    }
//数字交换法
    public long Fibonacci(long n) {
        long a = 0L;
        long b = 1L;
        while (n-- > 0) {
            b = a + b;
            a = b - a;
        }
        return a;
    }
//数组存储法
    public long Fibonacci1(long n) {
        long[] ans = new long[40];
        ans[0] = 0L;
        ans[1] = 1L;
        for (int i = 2; i <= n; i++) {
            ans[i] = ans[i - 1] + ans[i - 2];
        }
        return ans[Math.toIntExact(n)];
    }
//递归法
    public long Fibonacci2(long n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return 1;
        } else {
            return Fibonacci(n - 2) + Fibonacci(n - 1);
        }
    }

}
